package com.zrar.ak.gkpt.service.impl.record;

import com.zrar.ak.gkpt.dao.IBusinessTestVerifyDao;
import com.zrar.ak.gkpt.model.bo.BusinessTestVerifyBO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.record.IBusinessVerifyInfoService;
import com.zrar.ak.gkpt.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BusinessVerifyService
 * @Description: 测试验证Service实现类
 * @Author: CaoTao
 * @Create: 2020/04/07
 */
@Service
public class BusinessVerifyService implements IBusinessVerifyInfoService {

    @Autowired
    private IBusinessTestVerifyDao businessTestVerifyDao;

    /**
     * 测试验证基本信息保存
     *
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return JsonResult
     */
    @Override
    public JsonResult verifyInfoSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessTestVerifyBO businessTestVerifyBO = new BusinessTestVerifyBO();
        try {
            businessTestVerifyBO = CommonTools.jsonStrToObject(formDataJson, BusinessTestVerifyBO.class);
            String unitName = businessTestVerifyBO.getUnitName();
            // 判断建设单位是否已经存在测试验证信息
            BusinessTestVerifyBO businessTestVerifyBO1 = new BusinessTestVerifyBO();
            businessTestVerifyBO1 = businessTestVerifyDao.queryVerifyInfoByUnitName(unitName);
            if (businessTestVerifyBO1 != null) {
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_EXISTENT_DATA);
            } else {
                String creatorId = userVO.getUserId();
                Date createTime = new Date();
                String updateUserId = userVO.getUserName();
                Date updateTime = new Date();
                businessTestVerifyBO.setId(CommonTools.getUUID());
                businessTestVerifyBO.setCreatorId(creatorId);
                businessTestVerifyBO.setCreateTime(createTime);
                businessTestVerifyBO.setUpdateUserId(updateUserId);
                businessTestVerifyBO.setUpdateTime(updateTime);
                businessTestVerifyBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int insert = businessTestVerifyDao.insert(businessTestVerifyBO);
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    /**
     * 根据主键id删除测试验证信息
     *
     * @param jsonResult
     * @param id         主键id
     * @return JsonResult
     */
    @Override
    public JsonResult deleteVerifyInfoById(JsonResult jsonResult, String id) {
        int rowNum = businessTestVerifyDao.deleteVerifyById(id);
        jsonResult = CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    /**
     * 根据主键id查询测试验证信息
     *
     * @param jsonResult
     * @param id
     * @return JsonResult
     */
    @Override
    public JsonResult queryVerifyInfoById(JsonResult jsonResult, String id) {
        BusinessTestVerifyBO businessTestVerifyBO = new BusinessTestVerifyBO();
        // businessTestVerifyBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        // businessTestVerifyBO.setId(id);
        businessTestVerifyBO = businessTestVerifyDao.selectByPrimaryKey(id);
        jsonResult.setData(businessTestVerifyBO);
        CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    /**
     * 根据建设单位编码修改测试验证信息
     *
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return JsonResult
     */
    @Override
    public JsonResult verifyInfoUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessTestVerifyBO businessTestVerifyBO = new BusinessTestVerifyBO();
        String updateUserId = userVO.getUserName();
        Date updateTime = new Date();
        try {
            businessTestVerifyBO = CommonTools.jsonStrToObject(formDataJson, BusinessTestVerifyBO.class);
            BusinessTestVerifyBO verinfyInfo = businessTestVerifyDao.selectByPrimaryKey(businessTestVerifyBO.getId());

            if (verinfyInfo == null || BusinessEnum.IS_DELETE_Y.getValue().equals(verinfyInfo.getIsDelete())) {
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_NONEXISTENT_DATA);
            } else if (!verinfyInfo.getUnitId().equals(businessTestVerifyBO.getUnitId())) {
                //如果同一主键的数据库表记录的单位ID和传入数据的单位ID值不一样，则返回业务异常错误
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            } else {
                businessTestVerifyBO.setCreatorId(verinfyInfo.getCreatorId());
                businessTestVerifyBO.setCreateTime(verinfyInfo.getCreateTime());
                businessTestVerifyBO.setUpdateUserId(updateUserId);
                businessTestVerifyBO.setUpdateTime(updateTime);
                businessTestVerifyBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int count = businessTestVerifyDao.updateByPrimaryKey(businessTestVerifyBO);
                if (count > 0) {
                    CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
                } else {
                    CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    /**
     * 测试验证信息分页
     *
     * @param jsonResult
     * @param currentPage 当前页
     * @param pageSize    每页分几条
     * @param unitName    建设单位名称
     * @return JsonPageResult
     */
    @Override
    public JsonPageResult getVerifyInfoList(JsonPageResult jsonResult, int currentPage, int pageSize, String unitName) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if (StringUtils.isEmpty(unitName)) {
            unitName = null;
        }
        Map map = new HashMap();
        map.put("currentPage", currentPage);
        map.put("pageSize", pageSize);
        map.put("unitName", unitName);
        //返回前端的具体的数据
        List list = null;
        //符合展示条件的数据总数
        Integer total = null;
        total = businessTestVerifyDao.queryVerifyInfoShowNum(map);
        list = businessTestVerifyDao.pagingQueryVerifyInfoList(map);
        if (total == null) {
            total = 0;
        }
        //计算一共多少页
        Integer pageNum = (total % pageSize == 0) ? (total / pageSize) : (total / pageSize + 1);

        jsonResult.setData(list);
        jsonResult.setCurrentPage(currentPage);
        jsonResult.setPageSize(pageSize);
        jsonResult.setTotalCount(total);
        jsonResult.setTotalPage(pageNum);
        if (list == null || list.size() == 0) {
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_NONEXISTENT_DATA);
        } else {
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        }
        return jsonResult;
    }
}
